version development
#version 1.0


workflow ClinPatho_mNGS {
    input {
        String project
        Array[String] samples
        Array[File] fastqs
        Array[File] human_genome
        Array[File] plasmid_genome
        Directory kraken2_db
        File summary_tsv
        Array[String] ctrl_samples
        Int read_length = 50
        
    }
    scatter (i in range(length(samples))) {
        call Fastp {
            input:
            sample = samples[i],
            fastq = fastqs[i],
            read_length = read_length
        }
        call RemoveInterference as RemoveHuman {
            input:
            fastq = Fastp.cleaned_fastq,
            genome = human_genome,
            prefix = samples[i] + ".human",
        }
        call RemoveInterference as RemovePlasmid {
            input:
            fastq = RemoveHuman.unmap_fastq,
            genome = plasmid_genome,
            prefix = samples[i] + ".human.plasmid",
        }
        call Kraken2 {
            input:
            fastq = RemovePlasmid.unmap_fastq,
            kraken2_db = kraken2_db,
            sample = samples[i],
        }
        call Bracken {
            input:
            kraken2_report = Kraken2.report_tsv,
            kraken2_db = kraken2_db,
            read_length = read_length,
            sample = samples[i],
        }
        # call Pathogen {
        #     input:
        #     fastp_json = Fastp.fastp_json,
        #     kraken2_report = Kraken2.report_tsv,
        #     kraken2_fastq = Kraken2.fastq,
        #     bracken_out = Bracken.out_tsv,
        #     sample = samples[i]
        # }
        call ResFinder {
            input:
            sample = samples[i],
            fastq = RemoveHuman.unmap_fastq,
        }
        call VirulenceFinder {
            input:
            sample = samples[i],
            fastq = RemoveHuman.unmap_fastq,
        }
    }
    # call Result {
    #     input:
    #     pathogen_tsv = Pathogen.result_tsv,
    #     resfinder_tsv = ResFinder.result_tsv,
    #     virulencefinder_tsv = VirulenceFinder.result_tsv,
    #     summary_tsv = summary_tsv,
    #     ctrl_samples = ctrl_samples,
    #     prefix = project,
    # }
}

# 质控过滤
task Fastp {
    input {
        String docker = "registry.cn-shanghai.aliyuncs.com/kszy-biosoft/fastp:v0.23.1"
        Int cpu = 32
        String memory = "64G"
        String sample
        File fastq
        Int read_length
    }
    command {
        set -vex
        fastp -x -y -M 20 -n 1 -l 35 -b ${read_length} -w ${cpu} \
            -i ${fastq} -o ${sample}.cleaned.fastq.gz -j ${sample}.fastp.json -h ${sample}.fastp.html
    }
    output {
        File cleaned_fastq = "${sample}.cleaned.fastq.gz"
        File fastp_json = "${sample}.fastp.json"
    }
    runtime {
        docker: docker
        memory: memory
        cpu: cpu
    }
}

# 排除干扰数据 人源、质粒等
task RemoveInterference {
    input {
        String docker = "registry.cn-shanghai.aliyuncs.com/kszy-biosoft/bwa:v0.7.18-mamba"
        Int cpu = 32
        String memory = "64G"
        File fastq
        Array[File] genome
        String prefix
    }
    command {
        set -vex
        _entrypoint.sh bwa mem -t ${cpu} -M -T 20 ${genome[0]} ${fastq} | \
        _entrypoint.sh samtools fastq -@ ${cpu} -f 4 | gzip -c >${prefix}.unmap.fastq.gz
    }
    output {
        File unmap_fastq = "${prefix}.unmap.fastq.gz"
    }
    runtime {
        docker: docker
        memory: memory
        cpu: cpu
    }
}

# Kraken2
task Kraken2 {
    input {
        String docker = "registry.cn-shanghai.aliyuncs.com/kszy-biosoft/kraken2:v2.1.3"
        Int cpu = 64
        String memory = "128G"
        File fastq
        Directory kraken2_db
        String sample
    }
    command {
        set -vex
        kraken2 \
            --gzip-compressed \
            --db ${kraken2_db} \
            --threads ${cpu} \
            --output ${sample}.kraken2.out \
            --report ${sample}.kraken2.report \
            --unclassified-out ${sample}.kraken2.un.fastq \
            --classified-out ${sample}.kraken2.fastq \
            ${fastq}

    }
    output {
        File report_tsv = "${sample}.kraken2.report"
        File classified_fastq = "${sample}.kraken2.fastq"
    }
    runtime {
        docker: docker
        memory: memory
        cpu: cpu
    }
}

# Bracken
task Bracken {
    input {
        String docker = "registry.cn-shanghai.aliyuncs.com/kszy-biosoft/bracken:v2.9"
        Int cpu = 2
        String memory = "4G"
        File kraken2_report
        Directory kraken2_db
        String sample
        Int read_length
    }
    command {
        set -vex 
        for rank in $(cut -f 4 ${kraken2_report}|sort -u|awk '/^[GS]/')
        do
            bracken \
                -d ${kraken2_db} \
                -i ${kraken2_report} \
                -w ${sample}.bracken.$rank.report \
                -r ${read_length} \
                -l $rank \
                -t 0 \
                -o ${sample}.bracken.$rank.out
        done
    }
    output {
        Array[File] out_tsv = glob("${sample}.bracken.*.out")
    }
    runtime {
        docker: docker
        memory: memory
        cpu: cpu
    }
}

# Pathogen分类
task Pathogen {
    input {
        String docker = "clinpatho:latest"
        Int cpu = 4
        String memory = "8G"
        File fastp_json
        File kraken2_report
        File kraken2_fastq
        Array[File] bracken_out
        String sample
    }
    command {
        set -vex
        clinpatho kb  \
            -kraken2 ${kraken2_report} \
            -fastq ${kraken2_fastq} \
            -bracken ${sep=" " bracken_out} \
            -fastp ${fastp_json} \
            -out ${sample}
    }
    output {
        File result_tsv = "${sample}.pathogen.tsv"
        File reads_tsv = "${sample}.reads.tsv"
    }
    runtime {
        docker: docker
        memory: memory
        cpu: cpu
    }
}


# 耐药基因检测
task ResFinder {
    input {
        String docker = "registry.cn-shanghai.aliyuncs.com/kszy-biosoft/resfinder:v4.6.0"
        Int cpu = 4
        String memory = "8G"
        File fastq
        String sample
    }
    command {
        set -vex
        python -m resfinder -ifq ${fastq} -o . -acq -s "Other" -l 0.6 -t 0.8 && \
        cp ResFinder_results_tab.txt ${sample}.res.tsv
    }
    output {
        File result_tsv = "${sample}.res.tsv"
    }
    runtime {
        docker: docker
        memory: memory
        cpu: cpu
    }
}

# 毒力因子检测
task VirulenceFinder {
    input {
        String docker = "registry.cn-shanghai.aliyuncs.com/kszy-biosoft/virulencefinder:v3.1.0"
        Int cpu = 4
        String memory = "8G"
        File fastq
        String sample
    }
    command {
        set -vex
        python -m virulencefinder -ifq ${fastq} -o . -l 0.6 -t 0.8 -x && \
        cp results_tab.tsv ${sample}.vf.tsv
    }
    output {
        File result_tsv = "${sample}.vf.tsv"
    }
    runtime {
        docker: docker
        memory: memory
        cpu: cpu
    }
}

# 结果
task Result {
    input {
        String docker = "clinpatho:latest"
        Int cpu = 4
        String memory = "8G"
        Array[File] pathogen_tsv
        Array[File] resfinder_tsv
        Array[File] virulencefinder_tsv
        Array[String] ctrl_samples
        File summary_tsv
        String prefix
    }
    command {
        set -vex
        clinpatho rs \
            -patho ${sep=" " pathogen_tsv} \
            -res ${sep=" " resfinder_tsv} \
            -vf ${sep=" " virulencefinder_tsv} \
            -summary ${summary_tsv} \
            -out ${prefix}.result.zip \
            -ctrl ${sep=" " ctrl_samples}
    }
    output {
        File result_zip = "${prefix}.result.zip"
    }
    runtime {
        docker: docker
        memory: memory
        cpu: cpu
    }
}